uniform bvec4 textureUse;

uniform vec4 valueLambert;
uniform vec4 valueSpecular;
uniform vec4 valueFresnel;

//纹理
uniform sampler2D textureDiffuse;
uniform sampler2D textureEmissive;
uniform vec4 valueMapEmissive;

//Normal
uniform sampler2D textureNormal;
uniform sampler2D textureNormal2;
uniform vec4 valueNormal;
uniform vec4 valueNormal2;
uniform vec4 valueNormal2TilingOffset;


//MatCat
uniform sampler2D textureMatCatReflection;
uniform sampler2D textureMatCatDiffuse;
uniform sampler2D textureMatCatEmissive;
uniform vec4 valueMatCatReflection;
uniform vec4 valueMatCatDiffuse;
uniform vec4 valueMatCatEmissive;

//Matte
uniform sampler2D textureMatteDiffuse;
uniform sampler2D textureMatteSpecular;
uniform sampler2D textureMatteSmoothness;
uniform vec4 valueMatteDiffuse;
uniform vec4 valueMatteSpecular;
uniform vec4 valueMatteSmoothness;
uniform vec4 valueMatteDiffuseColor;
uniform vec4 valueMatteSpecularColor;
uniform vec4 valueMatteSmoothnessColor;

uniform vec4 valueMapDiffuse;
uniform vec4 valueLerp; //画面过度，第一个值，高光受镜面影响,菲涅耳受镜面影响


void main(void) {
	
	//Normal
	vec3 mapNormal = texture2D(textureNormal,outTexCoord0).rgb;

	vec2 valueNormal2UV = TRANSFORM_TEX(outTexCoord0,valueNormal2TilingOffset);
	vec3 mapNormal2 = valueNormal2TilingOffset.x>0.0 ? texture2D(textureNormal2,valueNormal2UV).rgb: vec3(0.0,0.0,0.0);
	mapNormal += mapNormal2;
	mapNormal = normalize(mapNormal);
	mapNormal = MapNormalToWorld(mapNormal);
	
	//纹理
	vec3 mapDiffuse = valueMapDiffuse.y>-1.0 ? ModulationRGB(texture2D(textureDiffuse, outTexCoord0 - mapNormal.xy * 0.0).rgb,valueMapDiffuse.xyz) : vec3(1.0, 1.0,
		1.0);
	vec3 mapEmissive = valueMapEmissive.z>0.0 ? ModulationRGB(texture2D(textureEmissive, outTexCoord0 - mapNormal.xy * 0.0).rgb,valueMapEmissive.xyz) : vec3(0.0, 0.0,
		0.0);
	
		
	//遮罩处理
	vec3 mapMatteDiffuse = valueMatteDiffuse.z>0.0?ModulationRGB(texture2D(textureMatteDiffuse, outTexCoord0 - mapNormal.xy * 0.0).rgb,valueMatteDiffuse.xyz):vec3(0.0, 0.0, 0.0);
	vec3 mapMatteSpecular = valueMatteSpecular.y>-1.0?ModulationRGB(texture2D(textureMatteSpecular, outTexCoord0 - mapNormal.xy * 0.0).rgb,valueMatteSpecular.xyz):vec3(1.0, 1.0, 1.0);
	vec3 mapMatteSmoothness = valueMatteSmoothness.y>-1.0?ModulationRGB(texture2D(textureMatteSmoothness, outTexCoord0 - mapNormal.xy * 0.0).rgb,valueMatteSmoothness.xyz):vec3(1.0, 1.0, 1.0);
	mapMatteDiffuse = standardRGB(mapMatteDiffuse);
	mapMatteDiffuse *= ColorRGBA(mapMatteDiffuse,valueMatteDiffuseColor);
	
		
	//MatCat处理
	vec3 matCatReflection = valueMatCatReflection.z > 0.0 ? ModulationRGB(texture2D(textureMatCatReflection, MatcapCoord(MapNormalIntensity(mapNormal, valueMatCatReflection.w))).rgb,valueMatCatReflection.xyz) : vec3(0.0, 0.0, 0.0);
	vec3 matCatDiffuse = valueMatCatDiffuse.y > -1.0 ? ModulationRGB(texture2D(textureMatCatDiffuse, MatcapCoord(MapNormalIntensity(mapNormal, valueMatCatDiffuse.w))).rgb,valueMatCatDiffuse.xyz) : vec3(1.0, 1.0, 1.0);
	vec3 matCatEmissive = valueMatCatEmissive.z > 0.0 ? ModulationRGB(texture2D(textureMatCatEmissive, MatcapCoord(MapNormalIntensity(mapNormal, valueMatCatEmissive.w))).rgb,valueMatCatEmissive.xyz) : vec3(0.0, 0.0, 0.0);
	matCatEmissive = standardRGB(matCatEmissive);
	matCatReflection = (vec3(0.5,0.5,0.5)-mapMatteSmoothness) + (matCatReflection*mapMatteSmoothness);
	//matCatReflection = standardRGB(matCatReflection);
	
	
	

	//兰伯特光照
	float lambert = Lambert(MapNormalIntensity(mapNormal, valueLambert.w), lightDir, valueLambert.xyz);
	//高光
	float specular = Specular(MapNormalIntensity(mapNormal, valueSpecular.w), lightDir, viewDir, valueSpecular.xyz);
	//菲涅耳
	float fresnel = Fresnel(MapNormalIntensity(mapNormal, valueFresnel.w), viewDir, valueFresnel.xyz);
//	fresnel = valueFresnel.y >-1.0?fresnel:1.0;
	
	vec3 diffuseRgb = ambientColor + (lightColor * (lambert+fresnel));
	
	diffuseRgb *= mapDiffuse * (matCatDiffuse);
	
	diffuseRgb = standardRGB(diffuseRgb-mapMatteDiffuse)+mapMatteDiffuse;
	
	diffuseRgb = standardRGB(diffuseRgb);
	

	vec3 specularRgb = vec3(specular * 0.6*mapDiffuse + specular * lightColor * 0.4);
	specularRgb *= LerpVec3(vec3(1.0, 1.0, 1.0), matCatReflection, valueLerp.x); //高光和镜面过度
	specularRgb *= mapMatteSpecular; //高光通道
	specularRgb = standardRGB(specularRgb);

//	vec3 fresnelRgb = fresnel*vec3(1.0,1.0,1.0);
//	fresnelRgb *= LerpVec3(vec3(1.0, 1.0, 1.0), matCatReflection, valueLerp.y);



	vec3 fragColorRgb = diffuseRgb + specularRgb + matCatEmissive;
	
	fragColorRgb = standardRGB(fragColorRgb-mapEmissive.x)+mapEmissive+(mapEmissive.x*mapDiffuse);	//加上自发光
	
	
//	fragColorRgb = standardRGB(fragColorRgb);

	gl_FragColor = vec4(fragColorRgb, 1.0);
	//	gl_FragColor =  vec4(vec3(specularRgb+fresnelRgb+diffuseRgb),1.0);
//		gl_FragColor =vec4(vec3(diffuseRgb+specularRgb+ matCatEmissive),1.0);
//	gl_FragColor =vec4(vec3(mapMatteSpecular),1.0);

}
